clear
use ${data}teacher_data

* Limit to Chicago region
keep if region==1

* Inflate binary outcomes by factor of 100
replace ans_satis = ans_satis * 100

eststo clear 
foreach c in ans_satis satisfaction min_gm   { 
	
	reg `c' da i.hs i.CorpsYear  $X if major_humanities==1, cluster(region_gl)
	eststo `c'_hum
	gen s=e(sample)
	estadd scalar p = 2*ttail(1,abs(_b[da]/_se[da])): `c'_hum
	qui su `c' if da==1 & s==1
	estadd scalar cm = round(r(mean) - _b[da],.001): `c'_hum
	drop s
	
	reg `c' da i.hs i.CorpsYear  $X if major_humanities==0, cluster(region_gl)
	eststo `c'_nohum
	gen s=e(sample)
	estadd scalar p = 2*ttail(1,abs(_b[da]/_se[da])): `c'_nohum
	qui su `c' if da==1 & s==1
	estadd scalar cm = round(r(mean) - _b[da],.001): `c'_nohum
	drop s	
}
	
 estout ans_satis_hum satisfaction_hum min_gm_hum   using ${results}tableA8_panelA.txt, replace ///
	keep(da) cells(b(star fmt(2) nostar)  se(par fmt(2))) ///
	collabels(,none) stat(p cm  N) 
	
 estout ans_satis_nohum satisfaction_nohum min_gm_nohum    using ${results}tableA8_panelA.txt, append ///
	keep(da) cells(b(star fmt(2) nostar)  se(par fmt(2))) ///
	collabels(,none) stat(p cm  N) 
	
	
* ---------------------------- *	
* Synthetic Control Humanities *
* ---------------------------- *
	
forv h=0/1 {
	

matrix output = J(2,3,.)

local c = 0
foreach outcome in  ans_satis  satisfaction min_gm   { 

local c=`c'+1



* Step 1: Convert to balanced panel of market level observations
clear
use ${data}teacher_data if major_humanities==`h'
 
* Inflate binary outcomes by factor of 100
replace ans_satis= ans_satis * 100

collapse `outcome' da  (sum) n, by(CorpsYear region hs)


egen group = group(region hs)


* Step 2: Run synthetic control
tsset group CorpsYear
synth  `outcome'   `outcome'(2009) `outcome'(2010) `outcome'(2011) `outcome'(2012) `outcome'(2013) ///
 `marketsize' ,  trunit(2) trperiod(2014) 

*synth  `outcome'   `outcome'(2009) `outcome'(2010) `outcome'(2011)  `marketsize' ,  trunit(2) trperiod(2014) fig

matrix Ysynth = e(Y_synthetic)
matrix Ytreat = e(Y_treated)
matrix weight_mat = e(W_weights)

* Save weights
preserve
clear
svmat weight_mat
ren weight_mat1 row
ren weight_mat2 `outcome'
tempfile `outcome'
save ``outcome''

* Step 3: Save effect in output matrix and make a pretty figure

clear
svmat Ysynth
svmat Ytreat
gen CorpsYear = 2008+_n
gen effect = Ytreat-Ysynth

matrix output[1,`c'] = effect[6]

 
* Step 4: Calculate relative post/pre RMSE
 restore 
forv m=1/8 {
 tempfile synth`m'
synth `outcome'  `outcome'(2009) `outcome'(2010) `outcome'(2011) `outcome'(2012) `outcome'(2013) ///
   `marketsize' `covariates'  ,  trunit(`m') trperiod(2014) keep(`synth`m'') replace
 matrix market`m' = e(RMSPE)
}

forv m=1/8 {
 
 use `synth`m'', clear


 keep _Y_treated _Y_synthetic _time
 drop if _time==.
 rename _time CorpsYear
 rename _Y_treated treat`m'
 rename _Y_synthetic counterfact`m'
 gen gap`m'=treat`m'-counterfact`m'
 sort CorpsYear
 save `synth`m'', replace
 
}

use `synth1'
forv m=2/8 {
 merge 1:1 CorpsYear using `synth`m''
 drop _merge
}
tempfile placebo
save `placebo', replace

forv m=1/8 {

 use `synth`m'', clear
 gen gap_sq = gap`m'*gap`m'
 egen postmean=mean(gap_sq) if CorpsYear==2014
 egen premean=mean(gap_sq) if CorpsYear<2014
 gen rmspe=sqrt(premean) if CorpsYear<2014
 replace rmspe=sqrt(postmean) if CorpsYear==2014
 gen ratio=rmspe/rmspe[_n-1] if CorpsYear==2014
 gen rmspe_post=sqrt(postmean) if CorpsYear==2014
 gen rmspe_pre=rmspe[_n-1] if CorpsYear==2014
 mkmat rmspe_pre rmspe_post ratio if CorpsYear==2014, matrix (market`m') 

}

* show post/pre-expansion RMSPE ratio for all states, generate histogram 
forv m=1/8 {
 matrix rownames market`m'= `m'
 matlist market`m', names(rows)
}
 mat market=market1\market2\market3\market4\market5\market6\market7\market8

clear
svmat market
gen market=_n
ren market1 rmspe_pre
ren market2 rmspe_post
ren market3 ratio
gsort -ratio
gen rank=_n
gen p=rank/_N

list rank p if market==2

sort market
matrix output[2,`c'] = p[2]


}
	



clear
svmat output
outsheet using ${results}tableA8_panelB_hum`h'_estimates.csv, comma names replace

	
}
	
exit
	